/**
 * Created by python on 19-3-2.
 */
let vm = new Vue({
    el: '#sub',
    data: {
        host: HOST,
        user_id: sessionStorage.user_id || localStorage.user_id,
        token: sessionStorage.token || localStorage.token,
        content: null,
        error_content:false
    },
    mounted: function () {
        CKEDITOR.replace('editor2');
    },
    methods: {
        check_content:function () {
            this.content = CKEDITOR.instances.editor2.document.getBody().getText();
            let len = this.content.length;
            if(len <2 || len > 140){
                layer.msg('长度在140字符以内，不能为空');
                this.error_content = true
            }
            else {
                this.error_content = false
            }
        },
        on_submit: function () {
            this.check_content();
            if(this.error_content == true){
                return
            }
            this.content = CKEDITOR.instances.editor2.document.getBody().getText();
            axios.post(this.host + '/spit/submitspit/', {
                    content: this.content,
                    user_id: this.user_id
                },
                {
                    headers: {
                        'Authorization': 'JWT ' + this.token
                    },
                    responseType: 'json',
                    withCredentials: true
                })
                .then(response => {
                    layer.msg(response.data.msg);
                    location.reload();
                })
                .catch(error => {
                    layer.msg(error.data.msg)
                })
        }
    }
});